package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.List;

public class LibraryManager {

    List<Book> books;
    private PrintStream printStream;

    public LibraryManager(List<Book> books, PrintStream printStream) {
        this.books = books;
        this.printStream = printStream;
    }

    public void checkOutBook(String title, String patron) {
        Book book = findBook(title);
        if (book != null && book.available()) {
            book.checkOut(patron);
            printStream.println("Thank you! Enjoy the book");
        } else {
            printStream.println("That book is not available.");
        }
    }

    private Book findBook(String title) {
        for (Book book : books) {
            if (book.toString().equals(title)) {
                return book;
            }
        }
        return null;
    }

    public void printBooks() {
        for (Book book : books) {
            printStream.println(book.toString());
        }
        printStream.println();
    }

    public void returnBook(String bookTitle) {
        Book book = findBook(bookTitle);
        if (book != null){
            book.checkOut("Library");
            printStream.println("Thank you for returning the book.");

        }
    }
}
